<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="供货商名称">
                <a-input allowClear placeholder="请输入供货商名称" v-model="queryParam.supplier_name" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="商品名称">
                <a-input allowClear placeholder="请输入商品名称" v-model="queryParam.name" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="商品分类">
                <a-cascader
                  :options="catList"
                  @change="onCascaderChange"
                  :fieldNames="{ label: 'name', value: 'id', children: 'children' }"
                  placeholder="请选择商品分类"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList">
          <a-radio-button value="0"> 未审核 </a-radio-button>
          <a-radio-button value="1"> 已通过 </a-radio-button>
          <a-radio-button value="2"> 已拒绝 </a-radio-button>
          <a-radio-button value="-1"> 全部 </a-radio-button>
        </a-radio-group>
        <div>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="handleAll('status', 1)">批量通过</a-menu-item>
              <a-menu-item @click="handleAll('status', 2)">批量拒绝</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>
      <a-alert type="info" show-icon style="margin: 10px 0" v-if="selectedRowKeys.length">
        <span slot="message">
          已选择 <span style="color: red">{{ selectedRowKeys.length }}</span> 项
        </span>
      </a-alert>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          current: queryParam.page,
          total: totalCount,
          pageSize: queryParam.limit,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :rowSelection="{
          selectedRowKeys: this.selectedRowKeys,
          onChange: (e) => {
            this.selectedRowKeys = e
          },
        }"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.pic_url" alt="" />
            </div>
            <div class="info">
              <a-tooltip placement="top">
                <template slot="title">
                  <span>{{ record.name }} </span>
                </template>
                <span style="color: #000">
                  {{ record.name }}
                </span>
              </a-tooltip>
            </div>
          </div>
        </div>

        <span slot="supplier_name" slot-scope="text">
          <a-tooltip placement="top">
            <template slot="title">
              <span>{{ text }} </span>
            </template>
            <span style="color: #000">
              {{ text }}
            </span>
          </a-tooltip>
        </span>

        <span slot="price" slot-scope="text"> ￥{{ text || 0 }} </span>

        <span slot="status" slot-scope="text">
          <div class="tagBox">
            <span class="tag" :style="text | orderStatus"></span>
            <span>{{ getStatus(text) }} </span>
          </div>
        </span>

        <div slot="action" slot-scope="scope">
          <a-button size="small" type="link" @click="see(scope.goods_id)">查看</a-button>
          <a-divider type="vertical" v-if="scope.status === '0'" />
          <a-dropdown v-if="scope.status === '0'">
            <a-button size="small" type="link"> 更多 <a-icon type="down" /> </a-button>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" @click="handelStatus(1, scope)">通过</a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;" @click="handelStatus(2, scope)">拒绝</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </div>
      </a-table>

      <a-modal
        title="审核"
        :confirm-loading="btnLoading"
        :destroyOnClose="true"
        v-model="showDeliver"
        :afterClose="closeModal"
        @ok="submit"
      >
        <a-form-model-item label="拒绝原因" :label-col="{ span: 6 }" :wrapper-col="{ span: 14 }"
          ><a-input v-model="desc" />
        </a-form-model-item>
      </a-modal>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { goodsAuditList, goodsAuditHandle } from '@/api/purchase/purchase'
import tabList from '@/views/saas/components/tabList.vue'
import { list as catListApi } from '@/api/purchase/cat'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '商品信息',
    scopedSlots: {
      customRender: 'name',
    },
    width: 200,
  },
  {
    title: '供货商',
    dataIndex: 'supplier_name',
    width: 200,
  },
  {
    title: '售价',
    dataIndex: 'price',
    scopedSlots: {
      customRender: 'price',
    },
    sorter: (a, b) => a.price - b.price,
  },
  {
    title: '库存',
    dataIndex: 'num',
    sorter: (a, b) => a.num - b.num,
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    width: 160,
    scopedSlots: {
      customRender: 'action',
    },
    fixed: 'right'
  },
]

export default {
  components: {
    tabList,
  },

  computed: {
    getStatus() {
      return (status) => {
        let str

        switch (parseInt(status)) {
          case 0:
            str = '未审核'
            break

          case 1:
            str = '已通过'
            break

          case 2:
            str = '已拒绝'
            break
        }

        return str
      }
    },
  },

  data() {
    return {
      tabList: [
        {
          key: 'saasPurchaseAuditList',
          name: '待审核商品',
        },
        {
          key: 'saasPurchaseAuditGoods',
          name: '云仓商品',
        },
        {
          key: 'saasSupplierCategory',
          name: '云仓分类',
        },
      ],

      // 搜索参数
      queryParam: {
        page: 1,
        limit: 10,
        name: '',
        status: '0',
        goods_id: '',
        supplier_name: '',
        cat_id: 0,
      },

      selectedRowKeys: [],
      columns,
      list: [],
      id: undefined,
      btnLoading: false,
      showDeliver: false,
      desc: '',
      catList: [],
    }
  },

  created() {
    if (this.$route.meta.queryParam) {
      this.queryParam = this.$route.meta.queryParam
    }

    this.getList()
    this.getCatList()
  },

  beforeRouteLeave(to, from, next) {
    from.meta.queryParam = this.queryParam
    next()
  },

  filters: {
    orderStatus(val) {
      if (val == 0) {
        return 'background-color:#faad14;'
      }

      if (val == 1) {
        return 'background-color:#65c817;'
      }

      if (val == 2) {
        return 'background-color:#ff0000;'
      }
    },

    sku(e) {
      return '-'
    },
  },

  methods: {
    getCatList() {
      catListApi({ is_show: -1 }).then((res) => {
        this.catList = res.data
      })
    },
    onCascaderChange(val) {
      this.queryParam.cat_id = val[val.length - 1]
    },
    async submit() {
      let { desc, id } = this
      if (desc === '') {
        return this.$message.warning('请输入拒绝原因')
      }

      this.btnLoading = true
      this.changeStatus({
        desc,
        ids: id,
        status: 2,
      })
    },

    // 关闭弹窗的回调
    closeModal() {
      this.id = undefined
      this.showDeliver = false
      this.desc = ''
    },

    see(id) {
      this.$router.push({
        path: `/saas/purchase/audit/edit?id=${id}`,
      })
    },

    // 分页请求
    tablePageChange(e) {
      this.queryParam.page = e
      this.getList()
    },

    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await goodsAuditList(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.list
        this.totalCount = res.data.count
      }
      hide()
    },

    // 批量操作
    handleAll(type, status) {
      let { selectedRowKeys, list } = this
      if (!selectedRowKeys.length) {
        this.$message.error('请至少选择一条数据~')
        return
      }

      let ids = selectedRowKeys.join(',')

      switch (type) {
        case 'status':
          this.handelStatus(status, {
            id: ids,
          })
          break
      }
    },

    handelStatus(status, scope) {
      if (status === 1) {
        this.changeStatus({
          ids: scope.id,
          status,
        })

        return
      }

      this.id = scope.id
      this.showDeliver = true
    },

    // 切换商品上下架删除状态
    async changeStatus(data) {
      const res = await goodsAuditHandle(data)
      this.btnLoading = false
      if (res.code == 0) {
        this.getList()
        this.$message.success('操作成功~')
        this.showDeliver = false
        return
      }

      this.$message.error(res.msg)
    },
  },
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    margin-top: 11px;
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
  .ant-btn {
    margin-top: 11px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}

.tagBox {
  display: flex;
  align-items: center;
}

.tag {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
}

::v-deep {
  .card_top_padding .ant-card-body .ant-tabs-bar {
    margin-bottom: 0;
  }
}
</style>
